using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZedGraph;

namespace ICP_MS.Plugin.Experiment;

public class FormIstd : Form
{
	private ExperimentDatas experiment = null;

	private ExperimentDB db = null;

	private int colorcount = 0;

	public Dictionary<string, Color> DicColor = new Dictionary<string, Color>();

	private IContainer components = null;

	private ZedGraphControl graph;

	private TableLayoutPanel tableLayoutPanel1;

	private FlowLayoutPanel pnlColor;

	public FormIstd()
	{
		InitializeComponent();
	}

	public void SetParams(ExperimentDatas experiment, ExperimentDB db)
	{
		this.experiment = experiment;
		this.db = db;
	}

	private void FormIstd_Load(object sender, EventArgs e)
	{
		base.WindowState = FormWindowState.Maximized;
		graph.GraphPane.XAxis.Title.Text = Lang.Txt("FormIstd.graph.GraphPane.XAxis.Title.Text ");
		graph.GraphPane.YAxis.Title.Text = Lang.Txt("FormIstd.graph.GraphPane.YAxis.Title.Text ");
		graph.GraphPane.Title.Text = Lang.Txt("FormIstd.graph.GraphPane.Title.Text ");
		graph.GraphPane.XAxis.Type = AxisType.Date;
		graph.GraphPane.XAxis.Scale.Format = "HH:mm:ss";
		DicColor.Clear();
		foreach (IsotopeInfos isotope in experiment.Isotopes)
		{
			if (isotope.inter)
			{
				DicColor.Add(isotope.symbol, Color.FromKnownColor((KnownColor)(141 + colorcount)));
				colorcount += 5;
			}
		}
		AddControls();
		experiment.isotopeChanged += experiment_isotopeChanged;
		experiment_isotopeChanged(null, null);
		Task.Factory.StartNew(delegate
		{
			DrawCurve();
		});
	}

	public void AddControls()
	{
		pnlColor.Controls.Clear();
		foreach (KeyValuePair<string, Color> dic in DicColor)
		{
			ColorCntl control = new ColorCntl(dic.Key, dic.Value);
			control.ColorChanged += control_ColorChanged;
			pnlColor.Controls.Add(control);
		}
		Button button = new Button();
		button.Text = Lang.Txt("FormIstd.Button.Refresh.Text");
		button.Click += button_Click;
		pnlColor.Controls.Add(button);
	}

	public void DrawCurve()
	{
		Invoke((EventHandler)delegate
		{
			graph.GraphPane.CurveList.Clear();
			foreach (IsotopeInfos isotope in experiment.Isotopes)
			{
				if (isotope.inter)
				{
					Color color = DicColor[isotope.symbol];
					LineItem lineItem = graph.GraphPane.AddCurve(isotope.symbol.ToString(), new PointPairList(), color, SymbolType.None);
					lineItem.Tag = isotope;
					IOrderedEnumerable<SamplesInfos> orderedEnumerable = from sample in experiment.Samples
						where sample.status == 1
						orderby sample.time
						select sample;
					foreach (SamplesInfos s in orderedEnumerable)
					{
						IEnumerable<StatisticResults> enumerable = experiment.Statistics.Where((StatisticResults ress) => ress.symbol == isotope.symbol && ress.sampleId == s.guid);
						if (enumerable != null && enumerable.Count() > 0)
						{
							lineItem.AddPoint(new XDate(s.time), enumerable.First().avgCPS);
						}
					}
				}
			}
			graph.GraphPane.AxisChange();
			Refresh();
		});
	}

	public void button_Click(object sender, EventArgs e)
	{
		Task.Factory.StartNew(delegate
		{
			DrawCurve();
		});
	}

	public void control_ColorChanged(object sender, EventArgs e)
	{
		string symbol = (sender as ColorCntl).Symbol;
		Color color = (sender as ColorCntl).color;
		DicColor[symbol] = color;
	}

	public void experiment_isotopeChanged(object sender, EventArgs e)
	{
		foreach (IsotopeInfos isotope in experiment.Isotopes)
		{
			if (!DicColor.ContainsKey(isotope.symbol) && isotope.inter)
			{
				DicColor.Add(isotope.symbol, Color.FromKnownColor((KnownColor)(141 + colorcount)));
				colorcount += 5;
			}
		}
		AddControls();
		Task.Factory.StartNew(delegate
		{
			DrawCurve();
		});
	}

	protected override void Dispose(bool disposing)
	{
		if (disposing && components != null)
		{
			components.Dispose();
		}
		base.Dispose(disposing);
	}

	private void InitializeComponent()
	{
		this.components = new System.ComponentModel.Container();
		this.graph = new ZedGraph.ZedGraphControl();
		this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
		this.pnlColor = new System.Windows.Forms.FlowLayoutPanel();
		this.tableLayoutPanel1.SuspendLayout();
		base.SuspendLayout();
		this.graph.Dock = System.Windows.Forms.DockStyle.Fill;
		this.graph.Location = new System.Drawing.Point(3, 3);
		this.graph.Name = "graph";
		this.graph.ScrollGrace = 0.0;
		this.graph.ScrollMaxX = 0.0;
		this.graph.ScrollMaxY = 0.0;
		this.graph.ScrollMaxY2 = 0.0;
		this.graph.ScrollMinX = 0.0;
		this.graph.ScrollMinY = 0.0;
		this.graph.ScrollMinY2 = 0.0;
		this.graph.Size = new System.Drawing.Size(688, 287);
		this.graph.TabIndex = 10;
		this.tableLayoutPanel1.ColumnCount = 1;
		this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50f));
		this.tableLayoutPanel1.Controls.Add(this.graph, 0, 0);
		this.tableLayoutPanel1.Controls.Add(this.pnlColor, 0, 1);
		this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
		this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0);
		this.tableLayoutPanel1.Name = "tableLayoutPanel1";
		this.tableLayoutPanel1.RowCount = 2;
		this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 76.76241f));
		this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 23.2376f));
		this.tableLayoutPanel1.Size = new System.Drawing.Size(694, 383);
		this.tableLayoutPanel1.TabIndex = 11;
		this.pnlColor.Dock = System.Windows.Forms.DockStyle.Fill;
		this.pnlColor.Location = new System.Drawing.Point(3, 296);
		this.pnlColor.Name = "pnlColor";
		this.pnlColor.Size = new System.Drawing.Size(688, 84);
		this.pnlColor.TabIndex = 11;
		base.AutoScaleDimensions = new System.Drawing.SizeF(6f, 12f);
		base.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
		base.ClientSize = new System.Drawing.Size(694, 383);
		base.Controls.Add(this.tableLayoutPanel1);
		base.Name = "FormIstd";
		base.ShowIcon = false;
		this.Text = "FormIstd";
		base.Load += new System.EventHandler(FormIstd_Load);
		this.tableLayoutPanel1.ResumeLayout(false);
		base.ResumeLayout(false);
	}
}
